package com.rg.dao;

import com.rg.model.GoodsModel;
import com.rg.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;
//数据访问对象类，用于操作商品数据。
//提供了多个方法，包括获取推荐商品列表、首页推荐商品、根据类型ID查询商品和根据商品ID查询商品。
public class Goodsdao {
    public List<GoodsModel> selectRecommandGoods() {
        List<GoodsModel> recomandList = null;
        try {
            String sql = "SELECT goods.goods_id goodsId, goods_name goodsName,cover_img coverImg,detail_image1 detailImg1,detail_image2 detailImg2,goods_price goodsPrice,goods_intro goodsIntro,goods_stock goodsStock,goods.type_id typeId FROM goods,goods_recommend WHERE goods_recommend.goods_id=goods.goods_id";
            QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
            recomandList = queryRunner.query(sql, new BeanListHandler<GoodsModel>(GoodsModel.class));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return recomandList;
    }

    public List<GoodsModel>selectIndexRecommend(){
        List<GoodsModel>IndexGoodsList=null;
        try{
            String sql="SELECT goods.goods_id goodsId, goods_name goodsName,cover_img coverImg,detail_image1 detailImg1,detail_image2 detailImg2,goods_price goodsPrice,goods_intro goodsIntro,goods_stock goodsStock,goods.type_id typeId FROM goods,goods_recommend WHERE goods_recommend.goods_id=goods.goods_id AND goods_recommend.recommendtype_id=2";
            QueryRunner queryRunner=new QueryRunner(DruidUtils.getDataSource());
            IndexGoodsList=queryRunner.query(sql,new BeanListHandler<GoodsModel>(GoodsModel.class));
        }catch(SQLException e){
            throw new RuntimeException(e);
        }
        return IndexGoodsList;
    }
    /*
     *根据点击的导航栏的类型ID查询相对应的商品
     */
    public List<GoodsModel> selectMenuGoodsByType(Integer TypeId){
        List<GoodsModel> MenuGoodsList=null;
        try{
            String sql="SELECT goods.goods_id goodsId, goods_name goodsName,cover_img coverImg ,detail_image1 detailImg1 ,detail_image2 detailImg2, goods_price goodsPrice,goods_intro goodsIntro,goods_stock goodsStock,goods.type_id typeId FROM goods,goods_type WHERE goods.type_id=goods_type.type_id and goods_type.type_id=?";
            QueryRunner queryRunner =new QueryRunner(DruidUtils.getDataSource());
            MenuGoodsList=queryRunner.query(sql,new BeanListHandler<GoodsModel>(GoodsModel.class),TypeId);
        }catch(SQLException e){
            throw new RuntimeException(e);
        }
        return MenuGoodsList;
    }
    /*
     *根据商品ID查询相对应的商品
     */
    public GoodsModel selectGoodsBygoodsId(Integer goodsId){
        GoodsModel goods=null;
        try{
            String sql="SELECT goods_id goodsId, goods_name goodsName, cover_img coverImg,detail_image1 detailImg1,detail_image2 detailImg2,goods_price goodsPrice,goods_intro goodsIntro,goods_stock goodsStock,type_id typeId FROM goods WHERE goods_id=?";
            QueryRunner queryRunner =new QueryRunner(DruidUtils.getDataSource());
            goods=queryRunner.query(sql,new BeanHandler<GoodsModel>(GoodsModel.class),goodsId);
        }catch(SQLException e){
            throw new RuntimeException(e);
        }
        return goods;
    }
}



